namespace LockStepFrame
{
    public class StepTimer
    {
        int frameCount;
        long tick_interval;
        long tick_start;

        System.Action action;

        public StepTimer(System.Action stepAction,int FPS = 10)
        {
            action = stepAction;
            frameCount = 0;
            tick_interval = TimeHelper.getTickInterval(FPS);
            tick_start = 0;
        }

        public void Start()
        {
            frameCount = 0;
            tick_start = TimeHelper.getTimeStampMS();
        }

        public void Update()
        {
            long now = TimeHelper.getTimeStampMS();
            int nextFrame = (int)((now - tick_start) / tick_interval);
            while(frameCount < nextFrame)
            {
                action();
                frameCount += 1;
            }
        }
    }
}
